package com.jojo.android.zxlib.tool;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

/**
 * 创建者 zx
 * 创建时间 2020/12/10 15:54
 */
public class ZxSpanTool {
    private final String TAG = "SpanTool";


    /**
     * 更改部分文字颜色
     *
     * @param text
     * @param start
     * @param end
     * @param color
     * @return
     */
    public static SpannableStringBuilder ColorSpan(CharSequence text, int start, int end, @ColorInt int color) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        spannableStringBuilder.setSpan(colorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }

    /**
     * 对文本字体进行缩放
     *
     * @param text
     * @param start
     * @param end
     * @param value
     * @return
     */
    public static SpannableStringBuilder ZoomSpan(CharSequence text, int start, int end, float value) {
        SpannableStringBuilder ss = new SpannableStringBuilder(text);
        RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(value);
        ss.setSpan(relativeSizeSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

    /**
     * 删除线
     *
     * @param text
     * @param start
     * @param end
     * @return
     */
    public static SpannableStringBuilder DeleteLineSpan(CharSequence text, int start, int end) {
        SpannableStringBuilder ss = new SpannableStringBuilder(text);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        ss.setSpan(strikethroughSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

    /**
     * 下划线
     *
     * @param text
     * @param start
     * @param end
     * @return
     */
    public static SpannableStringBuilder UnderlineSpan(CharSequence text, int start, int end) {
        SpannableStringBuilder ss = new SpannableStringBuilder(text);
        UnderlineSpan underlineSpan = new UnderlineSpan();
        ss.setSpan(underlineSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

    /**
     * 设置粗体、斜体
     *
     * @param text
     * @param start
     * @param end
     * @param fontStyle {@link android.graphics.Typeface}
     * @return
     */
    public static SpannableStringBuilder StyleSpan(CharSequence text, int start, int end, int fontStyle) {
        SpannableStringBuilder ss = new SpannableStringBuilder(text);
        StyleSpan styleSpan = new StyleSpan(fontStyle);
        ss.setSpan(styleSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

    /**
     * 点击事件
     *
     * @param text
     * @param start
     * @param end
     * @param clickListener
     * @return
     */
    public static SpannableStringBuilder ClickSpan(CharSequence text, int start, int end, final View.OnClickListener clickListener) {
        SpannableStringBuilder ss = new SpannableStringBuilder(text);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (clickListener != null) {
                    clickListener.onClick(widget);
                }
            }
        };
        ss.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }


    /**
     * 将highlights数组内的文字高亮展示
     * @param text
     * @param color
     * @param highlights
     * @return
     * @throws Exception
     */
    public static CharSequence getHighlightSpan(@NonNull CharSequence text,
                                                @ColorInt int color, String... highlights) throws Exception {
        for (String highlight : highlights) {
            int index = text.toString().indexOf(highlight);
            if (index == -1) {
                throw new Exception("not find highlight text：" + highlight);
            }
            text = ZxSpanTool.ColorSpan(text, index, index + highlight.length(), color);
        }
        return text;
    }

    /**
     *
     * @param text
     * @param v
     * @param zooms
     * @return
     * @throws Exception
     */
    public static CharSequence getZoomSpan(@NonNull CharSequence text,
                                                 float v, String... zooms) throws Exception {
        for (String zoom : zooms) {
            int index = text.toString().indexOf(zoom);
            if (index == -1) {
                throw new Exception("not find zoom text：" + zoom);
            }
            text = ZxSpanTool.ZoomSpan(text, index, index + zoom.length(), v);
        }
        return text;
    }

    /**
     * 批量设置富文本点击事件
     * @param text
     * @param onTextClick
     * @param clickTexts
     * @return
     * @throws Exception
     */
    public static CharSequence getClickSpan(@NonNull CharSequence text, final OnTextClick onTextClick, String... clickTexts) throws Exception {
        for (final String clickText : clickTexts) {
            final int index = text.toString().indexOf(clickText);
            if (index == -1) {
                throw new Exception("not find click text：" + clickText);
            }
            text = ZxSpanTool.ClickSpan(text, index, index + clickText.length(), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTextClick != null) {
                        onTextClick.onTextClick(clickText, index);
                    }
                }
            });
        }
        return text;
    }


    public interface OnTextClick {
        void onTextClick(String text, int startIndex);
    }
}
